package com.ebupt.migu.music.approve.controller;

import com.ebupt.migu.music.approve.entity.ReceiveMsgEvt;
import com.ebupt.migu.music.approve.entity.ReceiveMsgRt;
import com.ebupt.migu.music.approve.service.ApproveService;
import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.market.entity.ReceiveMsgResp;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Map;

import org.dom4j.io.SAXReader;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2019/10/10
 */
@Slf4j
@RestController
@RequestMapping("/approve")
public class ApproveController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ApproveService approveService;
    @Autowired
    private IDAuthUtils idAuthUtils;


    /**
     * 创建策略
     *
     * @return
     */
    @PostMapping("/list")
    public ResultObject queryApproveList() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return approveService.queryApproveList(params);
    }

    @PostMapping("/detail")
    public ResultObject queryApproveDetail() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        return approveService.queryApproveDetail(params);
    }

    /**
     * 提交审核
     *
     * @return
     */
    @PostMapping("/submit")
    public ResultObject submit() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        // 是否超级管理员
        params = idAuthUtils.isAdmin(params);
        return approveService.submit(params);
    }


    /**
     * 短信审核
     *
     * @return
     */
    @RequestMapping(path = "/receiveMsg")
    @ResponseBody
    public String receiveMsg(HttpServletRequest request, HttpServletResponse response) {
        ReceiveMsgEvt receiveMsgEvt = new ReceiveMsgEvt();
        StringWriter stringWriter = new StringWriter();
        try {
            //从http request对象中获取soap消息内容
            Reader readerx = request.getReader();
            StringBuilder sb = new StringBuilder();
            char[] buf = new char[1024];
            int len;
            while ((len = readerx.read(buf)) != -1) {
                sb.append(buf, 0, len);
            }
            readerx.close();
            //以上是从http request对象中获取soap消息内容
            //解析soap消息内容
            log.info("接收到的参数：" + sb.toString());
            SAXReader reader = new SAXReader();
            Document document = reader.read(new java.io.StringBufferInputStream(sb.toString()));
            Element root = document.getRootElement();
            Element body = root.element("Body");
            Element receiveMsg = body.element("receiveMsg");
            Element evt = receiveMsg.element("evt");
            receiveMsgEvt.setMsgContent(evt.elementText("msgContent"));
            ReceiveMsgRt receiveMsgRt = approveService.receiveMsg(receiveMsgEvt);
            JAXBContext jaxbContext = JAXBContext.newInstance(ReceiveMsgRt.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.marshal(receiveMsgRt, stringWriter);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return stringWriter.toString();
    }

    /**
     * 通过策略ID查询审批记录
     *
     * @return
     */
    @PostMapping("/queryByStrategyId")
    public ResultObject queryApproveByStrategyId() {
        Map<String, Object> params = ReqestParamUtil.getParams(request);
        return approveService.queryApproveByStrategyId(params);
    }
}
